<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link href="../../libs/mui.min.css" rel="stylesheet"/>
    <link rel="stylesheet" type="text/css" href="../../libs/app.css"/>
    <link href="../../css/main.css" rel="stylesheet"/>
    <link href="../../css/sure.css" rel="stylesheet"/>
</head>

<body>
<div class="head">
    <img class="mui-action-back" src="../../images/all-left.png" alt="">
    <p>支付</p>
</div>
<div class="content">
    <!--<div class="content-d">
    <p>学校贷名校贷</p>
    <div>
        <img src="../../images/school.png" alt="">
    </div>
    <p>共<span></span>件，合计：<span class="red">¥<span></span></span></p>
</div>-->
</div>
<div class="way">
    <p>支付方式</p>
    <div class="way1">
        <span class="quan"></span>
        <img class="img1" src="../../images/vip-zhi.png" alt="">
        <span>支付宝</span>
    </div>
    <div class="way2">
        <span class="quan"></span>
        <img class="img2" src="../../images/vip-wei.png" alt="">
        <span>微信</span>
    </div>
</div>
<div class="accound">
    <div class="all-pay">
        <p><span class="how"></span>合计：<span class="red">¥ <span class="allprice"></span></span>
        </p>
    </div>
    <div class="sure-pay">确认支付</div>
</div>
<script src="../../libs/mui.min.js"></script>
<script src="../../js/jquery-3.1.1.min.js"></script>
<script src="../../js/url.js"></script>
<script type="text/javascript">
    $('.allprice').text(localStorage.sureallprice);
    //			$('.how').text(localStorage.surehow);
    /*选择微信还是支付宝支付*/
    $('.way1').on('tap', function () {
        $('.way1>.quan').addClass('addred');
        $('.way2>.quan').removeClass('addred');
    });
    $('.way2').on('tap', function () {
        $('.way2>.quan').addClass('addred');
        $('.way1>.quan').removeClass('addred');
    });
    /*点击确认支付判断是微信还是支付宝*/
    $('.sure-pay').on('tap', function () {
        if ($('.way1>.quan').hasClass('addred')) {
            pay('alipay');
        } else if ($('.way2>.quan').hasClass('addred')) {
            pay('wxpay');
        } else {
            mui.toast('请选择支付方式');
        }
    });


    var ALIPAYSERVER = ur + 'rest/partnerController/ali-pay-partnerOrder';
    var WXPAYSERVER = ur + 'rest/partnerController/wechat-pay-partnerOrder';

    // 2. 发起支付请求
    function pay(id) {
        // 从服务器请求支付订单
        var PAYSERVER = '';
        if (id == 'alipay') {
            PAYSERVER = ALIPAYSERVER;
            mui.alert('微信端暂不支持支付宝支付╮(╯▽╰)╭');
            return;
        } else if (id == 'wxpay') {
            PAYSERVER = WXPAYSERVER;
        } else {
            // plus.nativeUI.alert("不支持此支付通道！", null, "捐赠");
            return;
        }
        $.ajax({ //获取支付信息
            url: PAYSERVER,
            headers: {
                "X-Token": localStorage.token
            },
            type: 'POST',
            dataType: 'json',
            data: {
                'id': localStorage.carid
            }
        })
            .done(function (res) {
                console.log('订单信息', res);
                if(res.meta.code == ERROR_OK) {
                    pays_web(res.data);
                }
            });
    }

    function pays_web(data) {
        function onBridgeReady() {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', data,
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                    } // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                }
            );
        }

        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        } else {
            onBridgeReady();
        }
    }

</script>

</body>

</html>